/**
 * 图像融合(幕布)
 * @param  viewer           Mars3d实例对象
 * 
 * @function addCurtain     添加图像
 * @function updateCurtain  更新图像信息
 * @function deleteCurtain  删除图像信息
 * 
 * @returns BQGIS_Curtain
 */
export function BQGIS_initCurtain(viewer){
    return new BQGIS_Curtain(viewer)
}


export function BQGIS_Curtain(viewer){
    this.viewer = viewer
    this.graphicLayer=null
    this.curtains=[]
    this.flydata=null
    this.flydatas=[]
}
function CreateVideoDom(){
    let videoElement = mars3d.DomUtil.create("video", "", document.body)
    videoElement.setAttribute("muted", "muted")
    videoElement.setAttribute("autoplay", "autoplay")
    videoElement.setAttribute("loop", "loop")
    videoElement.setAttribute("crossorigin", "")
    videoElement.setAttribute("controls", "")
    videoElement.style.display = "none"
    return videoElement
}
BQGIS_Curtain.prototype.SetVideoDom = function SetVideoDom(VideoImageScreen){
    let suffix = VideoImageScreen.url.substring(VideoImageScreen.url.lastIndexOf(".")+1)
    if(suffix==="flv"){
        if( window.flvjs.isSupported()){
            let videoElement =CreateVideoDom()
    
            let flvPlayer = window.flvjs.createPlayer({
                type: "flv",
                url: VideoImageScreen.url
            })
              flvPlayer.attachMediaElement(videoElement)
              flvPlayer.load()
              this.flydata = flvPlayer
              return{
                material: videoElement,
                angle:VideoImageScreen.fov,//水平张角
                angle2:VideoImageScreen.aspectRatio,//垂直张角
                distance:VideoImageScreen.dis,//视距
                heading:VideoImageScreen.azimuth,//方位角
                pitch:VideoImageScreen.pitchAngel,//俯仰角
                showFrustum:VideoImageScreen.frustumShow,//是否显示方框线
                stRotationDegree:VideoImageScreen.rotateDeg//视频或图片旋转角度
              }
        }
        else{
            return undefined
        }        
    }
    else if(suffix==="mp4"){
        let videoElement = CreateVideoDom()
        let sourceContainer = mars3d.DomUtil.create("source", "", videoElement)
        sourceContainer.setAttribute("src", VideoImageScreen.url)
        sourceContainer.setAttribute("type", "video/mp4")
        this.flydata = videoElement
        return{
            material:videoElement,//VideoImageScreen.url,
            angle:VideoImageScreen.fov,//水平张角
            angle2:VideoImageScreen.aspectRatio,//垂直张角
            distance:VideoImageScreen.dis,//视距
            heading:VideoImageScreen.azimuth,//方位角
            pitch:VideoImageScreen.pitchAngel,//俯仰角
            showFrustum:VideoImageScreen.frustumShow,//是否显示方框线
            stRotationDegree:VideoImageScreen.rotateDeg,//视频或图片旋转角度
            position:VideoImageScreen.position,
          }
    }
    else {
        return{
            material:VideoImageScreen.url,
            angle:VideoImageScreen.fov,//水平张角
            angle2:VideoImageScreen.aspectRatio,//垂直张角
            distance:VideoImageScreen.dis,//视距
            heading:VideoImageScreen.azimuth,//方位角
            pitch:VideoImageScreen.pitchAngel,//俯仰角
            showFrustum:VideoImageScreen.frustumShow,//是否显示方框线
            stRotationDegree:VideoImageScreen.rotateDeg,//视频或图片旋转角度
            position:VideoImageScreen.position,
        }
    }  
}
BQGIS_Curtain.prototype.addCurtain = function(VideoImageScreen,callBack=()=>{}){
    if(!this.graphicLayer){
        this.graphicLayer = new mars3d.layer.GraphicLayer()           
        this.viewer.addLayer(this.graphicLayer)
        this.graphicLayer.on(mars3d.EventType.click,function(event){
            console.log("监听layer，单击了对象", event.graphic)
            return callBack(event.graphic)
        })
    } 
    let videoObj = this.SetVideoDom(VideoImageScreen)
    if(videoObj){
         //构造投射体  
         const video2d = new mars3d.graphic.Video2D({
            position:VideoImageScreen.position,//位置
            targetPosition:VideoImageScreen.targetPosition,
            style:videoObj,
            id:VideoImageScreen.keyId
        })
       
        this.graphicLayer.addGraphic(video2d)     
        this.curtains.push(video2d)   
        this.flydatas.push({keyid:video2d.id,flydata:this.flydata})   
        return video2d
    }

}
BQGIS_Curtain.prototype.updateCurtain = function(VideoImageScreen){
    let data 
    if("keyType" in VideoImageScreen){
        data = this.curtains.filter((item)=>{
            return item[VideoImageScreen[keyType]]===VideoImageScreen.keyId
        })
    }
    else{
         data = this.curtains.filter((item)=>{
            return item.id===VideoImageScreen.keyId
        })
    }
    let suffix = VideoImageScreen.url.substring(VideoImageScreen.url.lastIndexOf(".")+1)
  
    if(data.length>0){
        data.filter((vaule)=>{
            if(suffix==="flv"){
                let fdata= flydatas.find((item)=>{
                    return item.keyid===vaule.id
                })
                if(fdata){
                    fdata.flvPlayer.url=VideoImageScreen.url
                }
                else{
                    return
                }
            }
            else if(suffix==="mp4"){
                let fdata= this.flydatas.find((item)=>{
                    return item.keyid===vaule.id
                })
                if(fdata){
                    fdata.flydata.children[0].src=VideoImageScreen.url
                    vaule.style={
                        //material:null,
                        material:fdata.flydata,
                        angle:VideoImageScreen.fov,
                        angle2:VideoImageScreen.aspectRatio,
                        distance:VideoImageScreen.dis,
                        heading:VideoImageScreen.azimuth,
                        pitch:VideoImageScreen.pitchAngel,
                        showFrustum:VideoImageScreen.frustumShow,
                        stRotationDegree:VideoImageScreen.rotateDeg,
                        position:VideoImageScreen.position,
                    }
                }
                else{
                    let videoElement = CreateVideoDom()
                    let sourceContainer = mars3d.DomUtil.create("source", "", videoElement)
                    sourceContainer.setAttribute("src", VideoImageScreen.url)
                    sourceContainer.setAttribute("type", "video/mp4")
                    let datadt= this.flydatas.find((item)=>{
                        return item.keyid===vaule.id
                    })
                    if(datadt){
                        let findex= this.flydatas.indexOf(datadt)
                        if(findex>-1){
                            this.flydatas.splice(findex,1,{keyid:vaule.id,flydata:videoElement});                   
                        } 
                        else{
                            this.flydatas.push({keyid:vaule.id,flydata:videoElement}) 
                        }                       
                    }
                    else{
                        this.flydatas.push({keyid:vaule.id,flydata:videoElement}) 
                    }
            
                    vaule.style={
                        //material:null,
                        material:videoElement,
                        angle:VideoImageScreen.fov,
                        angle2:VideoImageScreen.aspectRatio,
                        distance:VideoImageScreen.dis,
                        heading:VideoImageScreen.azimuth,
                        pitch:VideoImageScreen.pitchAngel,
                        showFrustum:VideoImageScreen.frustumShow,
                        stRotationDegree:VideoImageScreen.rotateDeg
                    }
                }
        
            }
            else{
                vaule.style={
                    material:VideoImageScreen.url,
                    angle:VideoImageScreen.fov,
                    angle2:VideoImageScreen.aspectRatio,
                    distance:VideoImageScreen.dis,
                    heading:VideoImageScreen.azimuth,
                    pitch:VideoImageScreen.pitchAngel,
                    showFrustum:VideoImageScreen.frustumShow,
                    stRotationDegree:VideoImageScreen.rotateDeg,
                    position:VideoImageScreen.position,
                }
            }
            

        })

    }
}
BQGIS_Curtain.prototype.deleteCurtain=function(ScreenObj){
    let data
    if("keyType" in ScreenObj){
        data = this.curtains.filter((item)=>{
            return item[ScreenObj["keyType"]]===ScreenObj.keyId
        })
    }
    else{
         data = this.curtains.filter((item)=>{
            return item.id===ScreenObj.keyId
        })
    }
    if(this.graphicLayer&&data.length>0){
        data.forEach((vaule)=>{
            this.graphicLayer.removeGraphic(vaule)
            let index= this.curtains.indexOf(vaule)
            if(index>-1){
                this.curtains.splice(index,1);
            }
            let fdata= this.flydatas.find((item)=>{
                return item.keyid===vaule.id
            })
            if(fdata){
                let findex= this.flydatas.indexOf(fdata)
                if(findex>-1){
                    this.flydatas.splice(findex,1);
                }
            }
        })
       
        
      
    }

}